package com.tanger.user;

import com.tanger.common.exception.AbstractController;
import com.tanger.common.result.ActionResult;
import com.tanger.common.utils.QRCodeUtil;
import com.tanger.common.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Description 用户控制层
 * @Author Hchenbin
 * @Date 2022/8/1 9:19
 * @Version 1.0
 */
@RestController
@RequestMapping("/tanger/api/test/user")
@Api(value = "测试:用户",tags="测试:用户")
public class UserController extends AbstractController{

    @Autowired
    private UserService userService;

    @ApiOperation(value = "用户列表")
    @GetMapping
    public ActionResult<User> findAll(){
        return ActionResult.buildResultSuccess(userService.findAll());
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true, dataType = "Long")})
    @ApiOperation(value = "用户详情")
    @GetMapping("/detail")
    public ActionResult<User> findUserByCreateDate(@RequestParam Long startTime,@RequestParam Long endTime){
        if (StringUtil.isNull(startTime) || StringUtil.isNull(endTime)) return ActionResult.buildResultError("参数缺失");
        return ActionResult.buildResultSuccess(userService.findUserByCreateDate(startTime,endTime));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "content", value = "内容", required = true, dataType = "String"),
            @ApiImplicitParam(name = "logoImage", value = "logo图片地址", required = false, dataType = "String")})
    @ApiOperation(value = "生成二维码")
    @GetMapping("/generateQRCode")
    public ActionResult generateQRCode(@RequestParam String content,@RequestParam(required = false) String logoImage) throws Exception {
        if (StringUtil.isNull(content)) return ActionResult.buildResultError("参数缺失");
        String url = QRCodeUtil.generateQRcode(content,true,logoImage);
        Map result = new HashMap();
        result.put("codeUrl",url);
        return ActionResult.buildResultSuccess(result);
    }

    @ApiOperation(value = "提交请假申请")
    @PostMapping("/submit")
    public ActionResult submit(@RequestBody Map map){
        if (Objects.isNull(map)) {
            return ActionResult.buildResultError("参数缺失");
        }
        return ActionResult.buildResultSuccess(userService.submit(map,"leave"));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "flowId", value = "流程ID", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "isPass", value = "是否通过(0.否 1.是)", required = false, paramType = "query", dataType = "String")})
    @ApiOperation(value = "发起审批")
    @PostMapping("/complate")
    public ActionResult complate(@RequestParam String flowId, Integer isPass){
        if (Objects.isNull(isPass) || StringUtil.isNull(flowId)) {
            return ActionResult.buildResultError("参数缺失");
        }
        return userService.complate(flowId,isPass);
    }

    @ApiOperation(value = "查询请假申请")
    @GetMapping("/findApply")
    public ActionResult findFlowAll(@RequestParam String roleCode){
        if (StringUtil.isNull(roleCode)) {
            return ActionResult.buildResultError("参数缺失");
        }
        List<Task> tasks = userService.findAll(roleCode);
        return ActionResult.buildResultSuccess(tasks);
    }
}

